#!/bin/bash
DEPLOY_DIR=$HOME/deploy
TOOL_BASE_DIR=/home/tool
APPS_BASE_DIR=$DEPLOY_DIR/apps
SCRIPTS_BASE_DIR=$DEPLOY_DIR/scripts
CONFIGS_BASE_DIR=$DEPLOY_DIR/configs

init(){
	PROJECT_SOURCE_DIR=$APPS_BASE_DIR/$APP_NAME
	PROJECT_DEPLOY_DIR=$PROJECT_SOURCE_DIR/deploy
}

package(){
	cd $PROJECT_SOURCE_DIR
	echo `pwd`
	svn update
	mvn clean
	mvn eclipse:clean
	mvn package -P${ENVIRONMENT} -Dmaven.test.skip=true
}

runtomcat(){
	TOMCAT_BASE_DIR=$PROJECT_DEPLOY_DIR/tomcat
	if [ -d $TOMCAT_BASE_DIR ]; then
		sh $SCRIPTS_BASE_DIR/tomcat/killws.sh $TOMCAT_BASE_DIR
		rm -rf $TOMCAT_BASE_DIR
	else
		mkdir $PROJECT_DEPLOY_DIR
	fi
	cp -r $TOOL_BASE_DIR/tomcat $TOMCAT_BASE_DIR
	cp $CONFIGS_BASE_DIR/$APP_NAME/tomcat_server.xml $TOMCAT_BASE_DIR/conf/server.xml
	cp -r $PROJECT_SOURCE_DIR/web/target/${APP_NAME}-web $TOMCAT_BASE_DIR/webapps/$APP_NAME
	sh $SCRIPTS_BASE_DIR/tomcat/startws.sh $TOMCAT_BASE_DIR
}

deploy(){
	init
	package
	runtomcat
}

echo "Please choose app:"
select item in `ls $APPS_BASE_DIR` Exit
do
	if [ $item = "Exit" ]; then
		exit
	else
		APP_NAME=$item
		break
	fi
done

echo "Please choose environment:"
. $CONFIGS_BASE_DIR/$APP_NAME/environment.sh
select item in `echo $environments` Exit
do
	if [ $item = "Exit" ]; then
		exit
	else
		ENVIRONMENT=$item
		break
	fi
done

deploy